<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017-10-08
 * Time: 16:44
 */

use yii\helpers\Url;

$web = Url::base();
$scriptUrl = \Yii::$app->request->scriptUrl;

?>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group col-lg-2">
                <a class="btn btn-default " type="button" data-toggle="modal" href="#detailModal" data-id='0'>
                    <i class="fa fa-plus"></i> 新增代理商
                </a>
            </div>

            <div class="pull-right">
                <div class="form-group">
                    <div id="searchInputInstitution"></div>

                </div>
            </div>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>代理商名称</th>
                    <th>省</th>
                    <th>市</th>
                    <th>区</th>
                    <th>创建时间</th>
                    <th>状态</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($items as $item) {
                    ?>
                    <tr>
                        <td><?= $item['agent_name'] ?></td>
                        <td><?= $item['agent_province_name'] ?></td>
                        <td><?= $item['agent_city_name'] ?></td>
                        <td><?= $item['agent_county_name'] ?></td>
                        <td><?= $item['create_datetime'] ?></td>
                        <td><?= $item['is_enabled'] == 1 ? '启用' : '停用' ?></td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-link" data-toggle="modal" data-id="<?= $item['id'] ?>"
                                   href="#detailModal">编辑</a>
                                <button class="btn btn-link btn-delete-item" data-id="<?= $item['id'] ?>">删除</button>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
    </div>
</div>

<div aria-hidden="true" role="dialog" tabindex="-1" id="detailModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">加盟商</h4>
            </div>
            <div class="modal-body" id="detail_content">
                正在加载数据....
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
                <button class="btn btn-primary" id="btnAddOk">确定</button>
            </div>
        </div>
    </div>
</div>

<script type="application/javascript">
    (function () {

        $('#btnAddOk').click(function () {
            var data = {};
            data.id = $("#agent_id").val();
            data.agent_name = $('#agent_name').val();
            data.agent_province_id = $('#agent_province').val();
            data.agent_city_id = $('#agent_city').val();
            data.agent_county_id = $('#agent_county').val();
            data.is_enabled = $("#is_enable").val();

            requestJson('agent-save-handler', data, true, function (responseData) {
                if (responseData.status == 0) {
                    window.location.reload();
                }
            })


        });

        $('#detailModal').on('show.bs.modal', function (e) {
            $("#detail_content").html('正在加载数据....');
            var id = e.relatedTarget.attributes['data-id'].value;
            var data = {};
            data.id = id;
            requestHtml('agent-detail-partial', data, true, function (responseHtml) {
                $("#detail_content").html(responseHtml);
            });
        });

        $('.btn-delete-item').click(function () {
            var result = confirm('是否确认删除此代理商?');
            if (result == true) {
                var id = $(this).attr('data-id');
                requestJson('agent-delete-handler', {'id': id}, true, function (responseData) {
                    if (responseData.status == 0) {
                        window.location.reload();
                    }
                });
            }
        });

    })(jQuery);

    //设置模态窗:当用户点击模态框外部时不会关闭、当初始化时不显示模态框。
    $(function () {
        $('#addModal').modal({
            backdrop: 'static', show: false
        });

        $('#detailModal').modal({
            backdrop: 'static', show: false
        });

    });
</script>
